package galnaat.szofttech;

import galnaat.szofttech.map.Fieldtype;

import java.util.Random;

public class map {
	private int nowX = 20;
	private int nowY = 8;

	Random generator = new Random();
	private static player Player = null;
	private String log = "";
	
	enum Fieldtype {
		x, F, _, O, M, v, i
		//Rejtett, Fal, Mezo, Te, Szorny, Kincs, Hiba
	}
	MyArray secret = new MyArray();
	MyArray visiable = new MyArray();
	
	public map() {
		// Hide everything
		for (int x = 0; x<mappage.sizeX; x++) {
			for (int y = 0; y<mappage.sizeY; y++) {
				visiable.set(x,y,Fieldtype.x);
			}
		}
		// Add the walls and others
		for (int x = 0; x<mappage.sizeX; x++) {
			for (int y = 0; y<mappage.sizeY; y++) {
				if (x == 0 || x == mappage.sizeX-1 || y == 0 || y == mappage.sizeY-1) {
					secret.set(x,y,Fieldtype.F);
				} else if (x == nowX && y == nowY) {
					secret.set(x, y, Fieldtype.O);
				} else {
					int i = generator.nextInt(100);
					if (i < 4) {
						secret.set(x, y, Fieldtype.M);
					}
					if (i < 8 && i >= 4) {
						secret.set(x, y, Fieldtype.v);
					}
					if (i < 15 && i >= 8) {
						secret.set(x, y, Fieldtype.F);
					}
				}
			}
        }
		// Call reDraw()
		reDraw();
	}
	
	public void setPlayer(player p) {
		if (Player==null) Player=p;
	}
	
	private void reDraw() {
		// Set visiable part
		for (int x = 0; x<mappage.sizeX; x++) {
			for (int y = 0; y<mappage.sizeY; y++) {
				if ((Math.abs(nowX-x)<=1 && Math.abs(nowY-y)<=1) ||
						visiable.get(x, y) != Fieldtype.x) {
					visiable.set(x, y, secret.get(x, y));
				}
			}
		}
	}
	
	public void Step(int x, int y) {
		if (Player.alive() && (
				(x == 1 && y == 0) ||
				(x == -1 && y == 0) ||
				(x == 0 && y == 1) ||
				(x == 0 && y == -1))
			) {
			if (secret.get(nowX+x, nowY+y)==Fieldtype._) {
				secret.set(nowX, nowY, Fieldtype._);
				nowX = nowX+x;
				nowY = nowY+y;
				secret.set(nowX, nowY, Fieldtype.O);
			}
			if (secret.get(nowX+x, nowY+y)==Fieldtype.M) {
				// Szorny
				if (Fight()) {
					secret.set(nowX, nowY, Fieldtype._);
					nowX = nowX+x;
					nowY = nowY+y;
					secret.set(nowX, nowY, Fieldtype.O);				
				}
			}
			if (secret.get(nowX+x, nowY+y)==Fieldtype.v) {
				// Kincs
				if (Diamond()) {
					secret.set(nowX, nowY, Fieldtype._);
					nowX = nowX+x;
					nowY = nowY+y;
					secret.set(nowX, nowY, Fieldtype.O);				
				}
			}
			reDraw();
		}
	}
	
	private boolean Fight() {
		fight f = new fight(Player);
		log += f.getLog();
		return f.getResult();
	}
	
	private boolean Diamond() {
		diamond d = new diamond(Player);
		log += d.getLog();
		return d.getResult();
	}
	
	public String toString() {
		String out = "";
        for (int y = 0; y<mappage.sizeY; y++) {
            for (int x = 0; x<mappage.sizeX; x++) {
            	out = out + visiable.get(x,y).toString();
            }
            out = out + "\n";
        }
        //System.out.println(out);
        return out;
	}
	
	public String PushLog() {
		String puffer = log;
		log = "";
		return puffer;
	}
}